﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.MissionViews.ReplayMissionView
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.MissionViews
{
  public class ReplayMissionView : MissionView
  {
    private float _resetTime;
    private bool _isInputOverridden;
    private ReplayMissionLogic _replayMissionLogic;

    public override void OnBehaviorInitialize()
    {
      base.OnBehaviorInitialize();
      this._resetTime = 0.0f;
      this._replayMissionLogic = this.Mission.GetMissionBehavior<ReplayMissionLogic>();
    }

    public override void OnPreMissionTick(float dt)
    {
      base.OnPreMissionTick(dt);
      this.Mission.Recorder.ProcessRecordUntilTime(this.Mission.CurrentTime - this._resetTime);
      int num = this._isInputOverridden ? 1 : 0;
      if (this.Mission.CurrentState != Mission.State.Continuing || !this.Mission.Recorder.IsEndOfRecord())
        return;
      if (MBEditor._isEditorMissionOn)
        MBEditor.LeaveEditMissionMode();
      else
        this.Mission.EndMission();
    }

    public void OverrideInput(bool isOverridden) => this._isInputOverridden = isOverridden;

    public void ResetReplay()
    {
      this._resetTime = this.Mission.CurrentTime;
      this.Mission.ResetMission();
      this.Mission.Teams.Clear();
      this.Mission.Recorder.RestartRecord();
      MBCommon.UnPauseGameEngine();
      this.Mission.Scene.TimeSpeed = 1f;
    }

    public void Rewind(float time)
    {
      this._resetTime = MathF.Min(this._resetTime + time, this.Mission.CurrentTime);
      this.Mission.ResetMission();
      this.Mission.Teams.Clear();
      this.Mission.Recorder.RestartRecord();
    }

    public void FastForward(float time) => this._resetTime -= time;

    public void Pause()
    {
      if (MBCommon.IsPaused || !this.Mission.Scene.TimeSpeed.ApproximatelyEqualsTo(1f))
        return;
      MBCommon.PauseGameEngine();
    }

    public void Resume()
    {
      if (!MBCommon.IsPaused && this.Mission.Scene.TimeSpeed.ApproximatelyEqualsTo(1f))
        return;
      MBCommon.UnPauseGameEngine();
      this.Mission.Scene.TimeSpeed = 1f;
    }
  }
}
